' Noncompliant: Insecure handling of user input
Sub Login()
Console.WriteLine("Enter your username: ")
Dim username As String = Console.ReadLine()
Console.WriteLine("Enter your password: ")
Dim password As String = Console.ReadLine()
'Insecure:Passwordisstoredasplaintext' Insecure: No input validation or sanitization
'Insecure:NoprotectionagainstbruteforceattacksIfusername="admin"AndAlsopassword="password"ThenConsole.WriteLine("Login successful!")ElseConsole.WriteLine("Login failed!")EndIfEndSub
✅ compliance
' Compliant: Secure handling of user input
Sub Login()
Console.WriteLine("Enter your username: ")
Dim username As String = Console.ReadLine()
Console.WriteLine("Enter your password: ")
Dim password As String = ReadPassword()
'Compliant:Passwordissecurelyhashedandstored' Compliant: Input validation and sanitization are implemented
'Compliant:Protectionagainstbruteforceattacks(e.g.,accountlockoutpolicy)IfValidateCredentials(username,password)ThenConsole.WriteLine("Login successful!")ElseConsole.WriteLine("Login failed!")EndIfEndSubFunctionReadPassword()AsStringDimpasswordAsNewSecureString()DimkeyInfoAsConsoleKeyInfoDokeyInfo=Console.ReadKey(intercept:=True)IfkeyInfo.Key=ConsoleKey.BackspaceAndAlsopassword.Length>0Thenpassword.RemoveAt(password.Length-1)Console.Write("\b \b")ElseIfkeyInfo.Key<>ConsoleKey.EnterThenpassword.AppendChar(keyInfo.KeyChar)Console.Write("*")EndIfLoopWhilekeyInfo.Key<>ConsoleKey.EnterConsole.WriteLine()DimunmanagedPasswordAsString=NothingTryunmanagedPassword=Marshal.PtrToStringBSTR(Marshal.SecureStringToBSTR(password))ReturnunmanagedPasswordFinallyIfunmanagedPasswordIsNotNothingThenArray.Clear(unmanagedPassword.ToCharArray(),0,unmanagedPassword.Length)EndIfpassword.Dispose()EndTryEndFunctionFunctionValidateCredentials(ByValusernameAsString,ByValpasswordAsString)AsBoolean' Compliant: Implement proper credential validation logic (e.g., check against secure database)
'Forthesakeofthisexample,usingasimplehardcodedcheckReturnusername="admin"AndAlsopassword="hashed_password"EndFunction